
from core.celery import celery_app, TaskMonitor
from app.celery_task.models.celery_task import CeleryTask
from app.bill.utils.bill_sync_utils import BillSyncUtils
from app.bill.handlers.bill_event_handler import BillEventHandler
from app.bill.handlers.oa_asset_handler import OaAssetHandler
from common.database import SessionLocal

from core.logger import logger



@celery_app.task(base=TaskMonitor, bind=True)
def bill_update_async(self, bill_cycle):
    # 获取任务信息
    tid = self.request.id
    task_name = self.name.split(".")[-1]
    session = SessionLocal()

    # 开始同步阿里云账单
    try:
        # 检查数据库任务信息是否创建
        result = CeleryTask.check_object_by_tid(session, tid)
        if not result:
            logger.error(f"tid:{tid} {task_name}failed, database info not exist")
            return False

        # 更新状态
        CeleryTask.update_task(session, tid, state="PROGRESS")

        # 启动任务
        bill_sync_utils = BillSyncUtils()
        result = bill_sync_utils.task_handle(session, tid, bill_cycle)
        if not result:
            logger.error(f"tid:{tid} => {task_name} failed")
            return False
    except Exception as e:
        logger.exception(f"tid:{tid} => {task_name} error=> {e}")
        return False
    finally:
        session.close()

    # 更新完成
    logger.info(f"tid:{tid} => {task_name} success")
    return True



@celery_app.task(base=TaskMonitor, bind=True)
def bill_event_from_work_order_async(self, bill_cycle):
    """
    更新资产工单事件的异步任务
    """

    # 获取任务信息
    tid = self.request.id
    task_name = self.name.split(".")[-1]
    session = SessionLocal()

    # 开始资产工单事件
    try:
        # 检查数据库任务信息是否创建
        result = CeleryTask.check_object_by_tid(session, tid)
        if not result:
            logger.error(f"tid:{tid} => progress failed, database info not exist")
            return False

        # 更新状态
        CeleryTask.update_task(session, tid, state="PROGRESS")

        # 启动任务
        bill_event_handler = BillEventHandler()
        result = bill_event_handler.task_update_from_work_order_handle(session, bill_cycle)
        if not result:
            logger.error(f"tid:{tid} => {task_name} failed")
            return False
    except Exception as e:
        logger.exception(f"tid:{tid} => {task_name} error=> {e}")
        return False
    finally:
        session.close()

    # 更新完成
    logger.info(f"tid:{tid} => {task_name} success")
    return True




@celery_app.task(base=TaskMonitor, bind=True)
def oa_asset_from_work_order_async(self, month):
    """
    更新oa云资源工单信息的异步任务
    """

    # 获取任务信息
    tid = self.request.id
    task_name = self.name.split(".")[-1]
    session = SessionLocal()

    # 开始资产工单事件
    try:
        # 检查数据库任务信息是否创建
        result = CeleryTask.check_object_by_tid(session, tid)
        if not result:
            logger.error(f"tid:{tid} => progress failed, database info not exist")
            return False

        # 更新状态
        CeleryTask.update_task(session, tid, state="PROGRESS")

        # 启动任务
        oa_asset_handler = OaAssetHandler()
        result = oa_asset_handler.task_update_from_work_order_handle(session, month)
        if not result:
            logger.error(f"tid:{tid} => {task_name} failed")
            return False
    except Exception as e:
        logger.exception(f"tid:{tid} => {task_name} error=> {e}")
        return False
    finally:
        session.close()

    # 更新完成
    logger.info(f"tid:{tid} => {task_name} success")
    return True






if __name__ == '__main__':
    pass
